package com.jjc.android.fragment

import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import com.hjq.toast.ToastUtils
import com.jjc.android.Constants
import com.jjc.android.GlobalManager
import com.jjc.android.R
import com.jjc.android.activity.DetectionActivity
import com.jjc.android.activity.FillProductInfoActivity
import com.jjc.android.activity.SamplingActivity
import com.jjc.android.activity.ScanActivity
import com.jjc.android.base.BaseActivity
import com.jjc.android.base.BaseLazyLoadFragment
import com.jjc.android.entity.KeyValuePair
import com.jjc.android.entity.LzyResponse
import com.jjc.android.entity.PageInfo
import com.jjc.android.entity.ScanCardDto
import com.jjc.android.entity.ScanDeviceDto
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.MultiLanguageUtil
import com.jjc.android.util.paramEx
import com.jjc.android.view.onClick
import com.lzy.okgo.OkGo

class BindReagentCardFragment : BaseLazyLoadFragment(){

    companion object{
        fun newInstance(testerId:String?,deviceSn:String):BindReagentCardFragment{
            val fragment = BindReagentCardFragment()
            fragment.setupArguments(Bundle().apply {
                putString("testerId",testerId)
                putString("deviceSn",deviceSn)
            })
            fragment.setInflateLazy(true)
            return fragment
        }
    }

    private lateinit var scanBtn: FrameLayout

    override fun getLayoutId(): Int = R.layout.fragment_bind_reagent_card
    private var testerId:String? = null
    private var deviceSn:String = ""
    override fun initView(view: View) {
        super.initView(view)
        initViews(view)
        scanBtn.onClick {
            ScanActivity.start(requireActivity(),Constants.ScanType.SCAN_CARD){
                fetchQrCode(it)
            }
        }
    }

    private fun fetchQrCode(code:String){
        OkGo.post<LzyResponse<PageInfo<ScanCardDto>>>(Constants.getFormList)
            .paramEx(mutableMapOf(
                "needpage" to 1,
                "page" to 1,
                "pagesize" to 1,
                "formheaduuid" to "611558d23f5e45eaafc974284e079206",
                "filters" to listOf<KeyValuePair>(
                    KeyValuePair("cpqjuttpyfgvakmf",MultiLanguageUtil.getInstance().getValue("d5a51385-3eeb-49fb-b6b7-64e77f2c5e92","3cc86818-6f66-4409-93ac-78c0ef70ab0f")),
                    KeyValuePair("tkynuu9ecdit0llv",code)
                )
            ),true)
            .tag(this@BindReagentCardFragment)
            .execute(object : SimpleHttpCallback<LzyResponse<PageInfo<ScanCardDto>>>(activity = activity as? BaseActivity){
                override fun onCallSuccess(response: LzyResponse<PageInfo<ScanCardDto>>) {
                    val scanDeviceDto = response.data?.first
                    if(scanDeviceDto!=null){
                        (activity as? DetectionActivity)?.let {activity->
                            activity.saveScanCardInfo(scanDeviceDto)
                            activity.nextStep()
                        }
                    }else{
                        ToastUtils.show(getString(R.string.no_find_card_txt))
                    }
                }

            })
    }

    override fun setupArguments(args: Bundle?) {
        testerId = args?.getString("testerId")
        deviceSn = args?.getString("deviceSn")?:""
    }

    private fun initViews(view: View) {
        scanBtn = view.findViewById(R.id.scanBtn)
    }

}